The device sharing check races when more than one file backed vbd is
authorrread@ubuntu.eng.hq.xensource.com <rread@ubuntu.eng.hq.xensource.com>
Mon, 28 Nov 2005 22:34:00 +0000 (15:34 -0700)
committerrread@ubuntu.eng.hq.xensource.com <rread@ubuntu.eng.hq.xensource.com>
Mon, 28 Nov 2005 22:34:00 +0000 (15:34 -0700)
configured. Both of the files pick the same free loop device in the for
loop, and then one fails to losetup.  This patch will now retry if
losetup fails until it succeeds or no more devices are available.

Signed-off-by: Robert Read <robert@xensource.com>
tools/examples/block

index 0a98344ca5862f99038d338d02d60398be2b5ffd..04a00432ffed630332d2baae9b22c78ec358c34d 100644 (file)
@@ -202,59 +202,61 @@ case "$command" in
 mount it read-write in a guest domain."
         fi
 
-        loopdev=''
 
-       for dev in /dev/loop*
-        do
-          if [ ! -b "$dev" ]
-          then
-            continue
-          fi
-
-          f=$(losetup "$dev" 2>/dev/null) || f='()'
-          f=$(echo "$f" | sed -e 's/.*(\(.*\)).*/\1/g')
-
-          log err "$file $f $dev"
-
-          if [ "$f" ]
-          then
-            # $dev is in use.  Check sharing.
-
-            if [ "$mode" == 'no' ]
+       while true
+        do 
+          loopdev=''
+          for dev in /dev/loop*
+          do
+            if [ ! -b "$dev" ]
             then
               continue
             fi
 
-            f=$(readlink -f "$f")
+            f=$(losetup "$dev" 2>/dev/null) || f='()'
+            f=$(echo "$f" | sed -e 's/.*(\(.*\)).*/\1/g')
+
+            log err "$file $f $dev"
 
-            if [ "$f" == "$file" ]
+            if [ "$f" ]
             then
-              check_file_sharing "$file" "$dev" "$mode"
+              # $dev is in use.  Check sharing.
+              if [ "$mode" == 'no' ]
+              then
+                continue
+              fi
+
+              f=$(readlink -f "$f")
+
+              if [ "$f" == "$file" ]
+              then
+                check_file_sharing "$file" "$dev" "$mode"
+              fi
+            else
+              # $dev is not in use, so we'll remember it for use later; we want
+              # to finish the sharing check first.
+              
+              if [ "$loopdev" == '' ]
+              then
+                loopdev="$dev"
+              fi
             fi
+          done
+
+          if [ "$loopdev" == '' ]
+          then
+            fatal 'Failed to find an unused loop device'
+          fi
+          if losetup "$loopdev" "$file"
+          then
+           log err "mapped $file using $loopdev"
+            xenstore_write "$XENBUS_PATH/node" "$loopdev"
+            write_dev "$loopdev"
+            exit 0
           else
-            # $dev is not in use, so we'll remember it for use later; we want
-            # to finish the sharing check first.
-            
-            if [ "$loopdev" == '' ]
-            then
-              loopdev="$dev"
-            fi
+            log err "losetup $loopdev $file failed, retry"
           fi
-        done
-
-        if [ "$loopdev" == '' ]
-        then
-          fatal 'Failed to find an unused loop device'
-        fi
-
-        if losetup "$loopdev" "$file"
-        then
-          xenstore_write "$XENBUS_PATH/node" "$loopdev"
-          write_dev "$loopdev"
-          exit 0
-        else
-          fatal "losetup $loopdev $file failed"
-        fi
+       done
        ;;
     esac
     ;;